import wepy from 'wepy'

export default class goods_detail extends wepy.mixin {
  data = {
    //商品id
    goods_id:0,
    //单个商品详情
    good:'',
    //标签页选中值
    active:1,
    // 地址
    address:null
  }

  methods = {
    //图片预览
    previwImage(current){
      let urls = [];
      this.good.pics.forEach(item => {
        urls.push(item.pics_big_url)
      })
      wepy.previewImage({
        urls,
        current
      })
    },
    //选择收货地址
    async getAddress(){
      const res = await wepy.chooseAddress().catch(err => err)
      if(res.errMsg !== "chooseAddress:ok") return wepy.baseToast("已取消选择")
      wepy.baseToast("选择地址成功")
      this.address = res
      wepy.setStorageSync("address",this.address)
    },
    //加入购物车
    addCar(){
      const {goods_id,goods_name,goods_price,goods_big_logo} = this.good;
      this.$parent.addCar(goods_id,goods_name,goods_price,goods_big_logo)
    }
  }

  computed = {
    // 选择收获地址
    addressStr(){
      if(this.address === null) return "请选择收货地址"
      const { provinceName,cityName,countyName } = this.address;
      return '' + provinceName+cityName+countyName
    },
    // 商品总数量的计算属性
    getTotal(){
      var total = 0
      this.$parent.globalData.cart.forEach(item => {
        total += item.count
      })
      return total;
    }
  }

  async getGoodDetail(){
    const {data} = await wepy.get("/goods/detail?goods_id=" + this.goods_id)
    if(data.meta.status !== 200) return wepy.baseToast()
    this.good = data.message;
    this.$apply();
  }

  onLoad(option){
    this.goods_id = option.goods_id
    this.address = wepy.getStorageSync("address") || null
    this.getGoodDetail()
  }
}
